package de.imdb250junkie.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import de.imdb250junkie.locale.LocaleUtils;
import de.imdb250junkie.utils.Movie;

/**
 * @author André Stannek
 * @version 2010-02-10
 */
public class Stats extends JPanel
{
    /**
     * 
     */
    private static final long serialVersionUID = 4471672466772573505L;

    /**
     * 
     */
    private final transient Movie[] top250;

    /**
     * 
     */
    private final transient JLabel seenCount, boughtCount, overallCount;

    /**
     * @param top
     *            top 250
     */
    public Stats(final Movie[] top)
    {
        super();
        top250 = top;
        boughtCount = new JLabel();
        seenCount = new JLabel();
        overallCount = new JLabel();
        final ResourceBundle locale = ResourceBundle.getBundle(
                "de.imdb250junkie.locale.GUIStrings", LocaleUtils.getLocale());

        update();

        this.setLayout(new GridBagLayout());

        final GridBagConstraints constraint = new GridBagConstraints();

        constraint.fill = GridBagConstraints.HORIZONTAL;

        constraint.gridwidth = 2;
        constraint.gridx = 0;
        constraint.gridy = 0;
        final JLabel head = new JLabel(locale.getString("stats"));
        head.setFont(new Font(null, Font.BOLD, 16));
        this.add(head);
        constraint.anchor = GridBagConstraints.LINE_START;
        constraint.gridx = 0;
        constraint.gridy = 1;
        constraint.weightx = 0.2;
        this.add(new JLabel(locale.getString("seen")), constraint);
        constraint.gridx = 1;
        constraint.gridy = 1;
        constraint.weightx = 0.8;
        this.add(seenCount, constraint);
        constraint.gridx = 0;
        constraint.gridy = 2;
        constraint.weightx = 0.2;
        this.add(new JLabel(locale.getString("bought")), constraint);
        constraint.gridx = 1;
        constraint.gridy = 2;
        constraint.weightx = 0.8;
        this.add(boughtCount, constraint);
        constraint.gridx = 0;
        constraint.gridy = 3;
        constraint.weightx = 0.2;
        this.add(new JLabel(locale.getString("overall")), constraint);
        constraint.gridx = 1;
        constraint.gridy = 3;
        constraint.weightx = 0.8;
        this.add(overallCount, constraint);
        this.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
        this.setMinimumSize(new Dimension(160, 200));
        this.setVisible(true);
    }

    /**
     * 
     */
    public final void update()
    {
        if (top250[0] != null)
        {
            int bought = 0;
            int seen = 0;
            for (final Movie movie : top250)
            {
                switch (movie.getState())
                {
                case BOUGHT:
                    bought++;
                    break;
                case SEEN:
                    seen++;
                    break;
                default:
                    break;
                }
            }
            boughtCount.setText(bought + " / "
                    + ((bought * 100) / top250.length) + "%");
            seenCount.setText(seen + " / " + ((seen * 100) / top250.length)
                    + "%");
            overallCount.setText((seen + bought) + " / "
                    + (((bought + seen) * 100) / top250.length) + "%");
        }
    }

}
